;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 unwox <me@unwox.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus home services himitsu-ssh)
  #:use-module (guix gexp)
  #:use-module (gnu home services)
  #:use-module (gnu home services shepherd)
  #:use-module ((guixrus packages himitsu) #:prefix rus:)
  #:export (home-himitsu-ssh-service-type))

(define (home-himitsu-ssh-shepherd-services config)
  (list (shepherd-service
         (provision '(himitsu-ssh))
         (requirement '(himitsu))
         (documentation "Run himitsu-ssh, an SSH-authentication daemon.")
         (start #~(make-forkexec-constructor
                   (list #$(file-append rus:himitsu-ssh "/bin/hissh-agent"))))
         (stop #~(make-kill-destructor)))))

(define (home-himitsu-ssh-profile-packages config)
  (list rus:himitsu-ssh))

(define (home-himitsu-ssh-environment-variables config)
  '(("SSH_AUTH_SOCK"
     . "${XDG_RUNTIME_DIR:-/run/user/$UID}/hissh-agent")))

(define home-himitsu-ssh-service-type
  (service-type
   (name 'home-himitsu-ssh)
   (default-value '())
   (extensions
    (list (service-extension home-shepherd-service-type
                             home-himitsu-ssh-shepherd-services)
          (service-extension home-environment-variables-service-type
                             home-himitsu-ssh-environment-variables)
          (service-extension home-profile-service-type
                             home-himitsu-ssh-profile-packages)))
   (description
    "Run himitsu-ssh, an SSH-authentication daemon.  To use this
service, add SSH keys to @command{himitsu} storage by running
@command{hissh-import < $HOME/.ssh/key-to-add}.")))
